package com.gzsxy.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gzsxy.model.CustomUser;
import com.gzsxy.model.SessionUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 会话用户工具类
 * @date 2021/11/9 17:55
 */
public class SessionUserUtil {


    /**
     * @description: 构建token附加信息
     * @param auth2Authentication  认证信息
     * @return
     */
    public static Map<String ,Object> buildTokenAdditionalInformation(OAuth2Authentication auth2Authentication){
        Map<String, Object> additionalInformation = new LinkedHashMap<>();

        try {

            CustomUser customUser = (CustomUser)auth2Authentication.getPrincipal();

            SessionUser sessionUser = new SessionUser();

            BeanUtils.copyProperties(customUser,sessionUser);
            if (null != sessionUser && StringUtils.isNotEmpty(sessionUser.getId())){
                Map<String,Object> sessionUserMap = JSON.parseObject(sessionUser.toString(),Map.class);
                additionalInformation.putAll(sessionUserMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return additionalInformation;
    }
}
